﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Profiles.cs" company="">
//   
// </copyright>
// <summary>
// Load and store the profiles of the users.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

/*
 * Author: Juan Mompean Esteban
 * Created: 6-12-2011
 */
namespace ITU_vs_Zombies.UsersInfo
{
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.IO;

    /// <summary>
    /// Load and store the profiles of the users.
    /// </summary>
    public static class Profiles
    {
        #region Constants and Fields

        /// <summary>
        /// The Path of the file with the stored profiles.
        /// </summary>
        public static readonly string Path = Directory.GetCurrentDirectory() + @"\Content\profiles.txt";

        #endregion

        #region Public Methods

        /// <summary>
        /// Load from a file the profiles of the users.
        /// </summary>
        /// <returns>
        /// A dictionary with the values loaded.
        /// </returns>
        public static Dictionary<string, string> LoadProfiles()
        {
            var profiles = new Dictionary<string, string>();
            if (!File.Exists(Path))
            {
                return profiles;
            }

            foreach (string line in File.ReadAllLines(Path))
            {
                try
                {
                    string[] splitLine = line.Split(':');
                    profiles.Add(splitLine[0], splitLine[1]);
                }
                catch
                {
                    // Ignore lines with errors
                }
            }

            return profiles;
        }

        /// <summary>
        /// The store profiles.
        /// </summary>
        /// <param name="profiles">
        /// The profiles.
        /// </param>
        public static void StoreProfiles(Dictionary<string, string> profiles)
        {
            Contract.Requires(profiles != null);
            var lines = new string[profiles.Count];
            int i = 0;
            foreach (var profile in profiles)
            {
                lines[i] = profile.Key + ":" + profile.Value;
                i++;
            }

            File.WriteAllLines(Path, lines);
        }

        #endregion
    }
}